﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Currency.TextUtils;

using CatUtils.Utils.SpecialUtils.Html;
using CatUtils.Utils.SpecialUtils.View;
using CatUtilsDll.Utils.Currency.TextUtils;
using CatUtilsDll.Utils.Currency.WebUtils;
using RabbitDownload.Helper;
using RabbitDownload.Helper.Currency;

using RabbitDownload.WebSite.Local.TagManger;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RabbitDownload.WebSite.Ehentai
{
    internal class EhentaiTag
    {
        public static string SiteDomain = "https://github.com/Mapaler/EhTagTranslator/wiki/";

        public static List<string> TagApi = new List<string> { "artist"
            , "female"  , "male"  , "parody"  , "character"  , "group"  , "language"  , "reclass"  , "misc" };

        public static String EhtagPath = StaticValueOld.DownloadPath + "EhentaiTag\\";
        private static int DataCount = 0;

        /// <summary>
        /// 异步启动批量下载Eh站数据，防止额界面卡顿。
        /// </summary>
        /// <param name="DownloadLink"></param>
        public async void DownloadDataBatchAsync(Label TipLable, ProgressBar progressBa)
        {
            await Task.Run(() => DownloadDataBatch(TipLable, progressBa));
        }

        /// <summary>
        /// 批量下载Eh站数据
        /// </summary>
        public void DownloadDataBatch(System.Windows.Forms.Label TipLable, ProgressBar progressBar)
        {
            FloderHelper.FloderExits(EhtagPath, true);

            DataCount = 0;

            progressBar.Maximum = TagApi.Count;
            int DataAllCount = 0;

            int RunCount = 0;
            foreach (string Line in TagApi)
            {
                progressBar.Value = ++RunCount;
                DataAllCount += DownloadData(Line, TipLable);
            }
            TipLable.Text = string.Format("更新完毕，总处理{0}条。", DataAllCount);
            MessageHelper.OpenFloderAlert(EhtagPath);
        }

        /// <summary>
        /// 下载Eh站数据
        /// </summary>
        /// <param name="Mode"></param>
        public int DownloadData(string Mode, Label TipLable)
        {
            string Url = SiteDomain + Mode;
            string Shtml = WebHelper.HttpPcGet(Url);
            List<string> TagList = new List<string>();
            foreach (var item in HtmlHelper.GetHtmlValueList(Shtml, "//tbody/tr"))
            {
                string Entag =

                 Transcoding.HtmlDecode(
                        TextHelper.ReplaceTextEnhance(
                        HtmlHelper.GetFirstText(item, "/tr/td[1]").Trim(), "(\ud83c[\udf00-\udfff])|(\ud83d[\udc00-\ude4f])|(\ud83d[\ude80-\udeff])"
                        , ""));

                string Cntag =
                   Transcoding.HtmlDecode(TextHelper.ReplaceTextEnhance(
                         HtmlHelper.GetFirstText(item, "/tr/td[2]").Trim(), "(\ud83c[\udf00-\udfff])|(\ud83d[\udc00-\ude4f])|(\ud83d[\ude80-\udeff])"
                         , ""));

                if (Cntag == "" || Entag == "")
                    continue;
                TipLable.Text = string.Format("当前已处理数据{0}条。", DataCount++);

                TagList.Add(string.Format(LocalTagStatcivalue.TextMode, "", Cntag, "", Entag, "", ""));
            }
            FileHelper.WriteList(EhtagPath + Mode + ".txt", TagList);
            return DataCount;
        }
    }
}